package com.ler.yrmall.order.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ler.yrmall.basic.Form;
import com.ler.yrmall.exception.BusinessException;
import com.ler.yrmall.order.entity.OrderEntity;
import com.ler.yrmall.order.entity.OrderItemEntity;
import com.ler.yrmall.order.entity.OrderReturnApplyEntity;
import com.ler.yrmall.order.form.ReturnApplyForm;
import com.ler.yrmall.order.mapper.OrderReturnApplyMapper;
import com.ler.yrmall.order.service.OrderItemService;
import com.ler.yrmall.order.service.OrderReturnApplyService;
import com.ler.yrmall.order.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author leron
 */
@Service
public class OrderReturnApplyServiceImpl extends ServiceImpl<OrderReturnApplyMapper, OrderReturnApplyEntity> implements OrderReturnApplyService {

    @Resource
    private OrderService orderService;

    @Resource
    private OrderItemService orderItemService;

    @Override
    public IPage<OrderReturnApplyEntity> find(Form request) {
        return this.page(request.getPage());
    }

    @Override
    public void apply(ReturnApplyForm request) {
        OrderItemEntity item = orderItemService.getById(request.getItemId());
        if (item == null) {
            throw new BusinessException("订单不存在");
        }
        OrderEntity order = orderService.getOrderByOrderSn(item.getOrderSn());
        List<OrderReturnApplyEntity> processing = this.list(Wrappers.<OrderReturnApplyEntity>lambdaQuery()
                .eq(OrderReturnApplyEntity::getOrderSn, item.getOrderSn())
                .eq(OrderReturnApplyEntity::getStatus, OrderReturnApplyEntity.ReturnStatus.PENDING.getCode()));
        if (!processing.isEmpty()) {
            throw new BusinessException("当前订单下有正在处理的售后单，请等单处理完成后再次发起申请");
        }
        OrderReturnApplyEntity returnApply = new OrderReturnApplyEntity();
        returnApply.setOrderId(item.getOrderId());
        returnApply.setItemId(item.getId());
        returnApply.setSkuId(item.getSkuId());
        returnApply.setOrderSn(item.getOrderSn());
        returnApply.setMemberUsername(order.getMemberUsername());
        returnApply.setReturnAmount(item.getSkuPrice().multiply(BigDecimal.valueOf(request.getRefundCount())));
        returnApply.setStatus(OrderReturnApplyEntity.ReturnStatus.PENDING.getCode());
        returnApply.setSkuImg(item.getSkuName());
        returnApply.setSkuName(item.getSkuName());
        returnApply.setSkuAttrsVals(item.getSkuAttrsVals());
        returnApply.setRefundCount(request.getRefundCount());
        returnApply.setSkuPrice(item.getSkuPrice());
        returnApply.setSkuRealPrice(item.getRealAmount());
        returnApply.setReason(request.getReason());
        returnApply.insert();
    }

    @Override
    public void pass(Long refundId) {
        // 优惠券返还
        // 积分返还
    }

}